package com.lmx.messagetemplate.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.lmx.messagetemplate.R;
import com.lmx.messagetemplate.bean.MessageEvent;
import com.lmx.messagetemplate.bean.MessageTemplate;
import com.lmx.messagetemplate.ui.adapter.TitleAdapterRv;
import com.lmx.messagetemplate.util.SharedPreUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "测试";
    private RecyclerView recyclerView;
    private RelativeLayout rlPrompt;
    private FloatingActionButton floatingActionButton;
    private TitleAdapterRv adapterRv;
    private List<MessageTemplate> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        getData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
            Log.d(TAG, "1注册成功！");
        }
    }

    private void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        adapterRv = new TitleAdapterRv(this);
        recyclerView.setAdapter(adapterRv);
        rlPrompt = findViewById(R.id.rl_prompt);
        floatingActionButton = findViewById(R.id.floatingActionButton);
    }

    private void initEvent() {
        floatingActionButton.setOnClickListener(v -> {
            startActivity(new Intent(this, CreateTemplateActivity.class));
        });

        adapterRv.setOnItemClickListener((v, template) -> {
            Intent intent = new Intent(this, MessageActivity.class);
            intent.putExtra("data", template);
            startActivity(intent);
        });
    }

    private void getData() {
        data = LitePal.findAll(MessageTemplate.class, true);
        Log.d(TAG, "data:" + data);
        if (data != null && data.size() > 0) {
            adapterRv.setData(data);
            rlPrompt.setVisibility(View.GONE);
        } else {
            rlPrompt.setVisibility(View.VISIBLE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent event) {
        MessageTemplate template = event.getTemplate();
        if (data != null) {
            data.add(template);
            adapterRv.setData(data);
            rlPrompt.setVisibility(View.GONE);
        } else {
            rlPrompt.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}